package com.ifcar.app.activity.topic;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import com.ifcar.app.adapter.VoteWithTabAdapter;
import com.ifcar.app.fragment.NRLoadingFrament;
import com.ifcar.app.model.Topic;
import com.ifcar.app.model.Vote;
import com.ifcar.app.activity.R;

/**
 * 投票数据获取数据
 * @author xali
 */
public abstract class VoteDataDeal extends NRLoadingFrament {
	
	public VoteWithTabAdapter mAdapter; 
	public List<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();
	public View mLoaddingView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRefreshTag(Topic.TOPIC_VOTE);

        if (savedInstanceState != null) {
            ArrayList<String> voteArray = savedInstanceState.getStringArrayList("voteArray");
            mData.addAll(Vote.stringArray2Data(voteArray));
        }
    }
    
    @Override
    public void onViewCreatedNR(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
    	mLoaddingView = view.findViewById(R.id.vote_loadding);
	}

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        ArrayList<String> voteArray = Vote.data2StringArray(mData);

        if (voteArray != null && voteArray.size() > 0) {
            outState.putStringArrayList("voteArray", voteArray);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        List<Map<String, Object>> data = getApplication().getVoteList();
        if (data != null) {
            getApplication().setVoteList(null);
            setData(data);
        }
    }   
    
    @Override
    protected void onLoading() {
        super.onLoading();

        Message msg = getHandler().obtainMessage(MSG_LOADING);
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        data.addAll(mData);
        msg.obj = data;
        getHandler().dispatchMessage(msg);
    }

    @Override
    protected void onHandleLoading(Object result) {
        super.onHandleLoading(result);
        List<Map<String, Object>> list = (List<Map<String, Object>>) result;
        setData(list);

        if (mData != null && mData.size() > 0) {
            if (isNeedRefresh()) {
                getHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refresh();
                    }
                }, 200);
            }
        } else {
            refresh();
        }

    }

    @Override
    protected void onRefreshing() {
        super.onRefreshing();
        mLoaddingView.setVisibility(View.VISIBLE);
        final Context context = getActivity();
        new Thread() {
            public void run() {
                Message msg = getHandler().obtainMessage(MSG_REFRESHING);
                msg.obj = Vote.getVoteList(context);
                getHandler().sendMessage(msg);
            };
        }.start();
    }

    @Override
    protected void onHandleRefreshing(Object result) {
        super.onHandleRefreshing(result);
        List<Map<String, Object>> data = (List<Map<String, Object>>) result;
        mLoaddingView.setVisibility(View.GONE);
        if (data != null && data.size() != 0) {
            setData(data);
        } else {
            Toast.makeText(getActivity(), R.string.msg_loading_failed, Toast.LENGTH_SHORT).show();
        }
    }
    
    private void setData(List<Map<String, Object>> data) {
    	
    	
        if (data != null && data.size() > 0) {
            mData.clear();
            mData.addAll(data);
        }
        
        invalidDataView();
    }
    
    public abstract void invalidDataView();
}
